<?php
require_once 'util/class.dbservice.php';

class OutboxDao {

	public function addOutboxItem($id, $userId, $noteId, $type, $timestamp) {
		try {
			$collection = $this->getCollection($userId);
			if(!$collection)
				return;

			$collection->insert(array('_id'=>$id, 'uid'=>$userId, 'nid'=>$noteId,
					'tp'=>$type, 'ts'=>$timestamp));
		} catch(Exception $e) {
			syslog(LOG_ERR, "OutboxDao.addOutboxItem error, reason=".$e);
		}
	}
	
	public function getOutboxList($userId, $type, $pageSize, $sinceNoteId) {
		try {
			$collection = $this->getCollection($userId);
			if(!$collection)
				return;
			
			if($type!=-1) {
				if($sinceNoteId==0)
					$cursor=$collection->find(array('uid'=>$userId, 'tp'=>$type, 'nid'=>array('$gt'=>$sinceNoteId)),
							array('_id'=>0))->sort(array('nid'=>-1))->limit($pageSize);
				else
					$cursor=$collection->find(array('uid'=>$userId, 'tp'=>$type, 'nid'=>array('$lt'=>$sinceNoteId)),
							array('_id'=>0))->sort(array('nid'=>-1))->limit($pageSize);
			}
			else {
				if($sinceNoteId==0)
					$cursor=$collection->find(array('uid'=>$userId, 'nid'=>array('$gt'=>$sinceNoteId)),
							array('_id'=>0))->sort(array('ts'=>-1))->limit($pageSize);
				else
					$cursor=$collection->find(array('uid'=>$userId, 'nid'=>array('$lt'=>$sinceNoteId)),
							array('_id'=>0))->sort(array('ts'=>-1))->limit($pageSize);
			}
	
			$result=array();
	 		foreach ($cursor as $doc) {
	 			$result[]=$doc;
	 		}
			return $result;
	
		} catch(Exception $e) {
			syslog(LOG_ERR, "OutboxDao.getOutboxList error, reason=".$e);
			return null;
		}
	}
	
	public function getOutboxListByTimestamp($userId, $type, $pageSize, $sinceTimestamp) {
		try {
			$collection = $this->getCollection($userId);
			if(!$collection)
				return;
	
			if($type!=-1) {
				if($sinceTimestamp==0)
					$cursor=$collection->find(array('uid'=>$userId, 'tp'=>$type, 'ts'=>array('$gt'=>$sinceTimestamp)),
							array('_id'=>0))->sort(array('ts'=>-1))->limit($pageSize);
				else
					$cursor=$collection->find(array('uid'=>$userId, 'tp'=>$type, 'ts'=>array('$lt'=>$sinceTimestamp)),
							array('_id'=>0))->sort(array('ts'=>-1))->limit($pageSize);
			}
			else {
				if($sinceTimestamp==0)
					$cursor=$collection->find(array('uid'=>$userId, 'ts'=>array('$gt'=>$sinceTimestamp)),
							array('_id'=>0))->sort(array('ts'=>-1))->limit($pageSize);
				else
					$cursor=$collection->find(array('uid'=>$userId, 'ts'=>array('$lt'=>$sinceTimestamp)),
							array('_id'=>0))->sort(array('ts'=>-1))->limit($pageSize);
			}
	
			$result=array();
	 		foreach ($cursor as $doc) {
	 			$result[]=$doc;
	 		}
			return $result;
	
		} catch(Exception $e) {
			syslog(LOG_ERR, "OutboxDao.getOutboxListByTimestamp error, reason=".$e);
			return null;
		}
	}

	private function getCollection($userId) {
		$db=DBService::getDbById('user', $userId);
		$collectionName = DBService::getCollectionNameById('outbox', $userId);
		return $db->$collectionName;
	}
}